//
//  DescriptorView.m
//  FileBrowser
//
//  Created by Basuke on 08/03/18.
//  Copyright 2008 Basuke's. All rights reserved.
//

#import "DescriptorView.h"
#import "BuilderHelper.h"


@interface DescriptorView(Local)

- (DescriptorView *)appendView:(UIView *)aView;
- (DescriptorView *)appendView:(UIView *)aView leftMargin:(CGFloat)leftMargin;
- (DescriptorView *)addMargin;

@end

#define kDescriptorView_LeftMargin 6.0

@implementation DescriptorView

@synthesize origin;

- (id)initWithFrame:(CGRect)frame {
	if (self = [super initWithFrame:frame]) {
		self.origin = CGPointZero;
	}
	return self;
}

- (DescriptorView *)addSubview:(UIView *)aView withLabel:(NSString *)aLabel {
	UILabel *labelView = [BuilderHelper titleLabelWith:aLabel at:CGPointZero];
	[self appendView:labelView];
	
	[self appendView:aView leftMargin:kDescriptorView_LeftMargin];
	
	return self;
}

- (DescriptorView *)addString:(NSString *)aString withLabel:(NSString *)aLabel {
	UILabel *labelView = [BuilderHelper titleLabelWith:aLabel at:CGPointZero];
	[self appendView:labelView];
	
	labelView = [BuilderHelper labelWith:aString at:CGPointZero];
	[self appendView:labelView leftMargin:kDescriptorView_LeftMargin];
	
	return self;
}

- (DescriptorView *)appendView:(UIView *)aView {
	return [self appendView:aView leftMargin:0.0];
}

- (DescriptorView *)appendView:(UIView *)aView leftMargin:(CGFloat)marginLeft {
	[aView moveTo:CGPointMake(marginLeft, self.origin.y)];
	origin.y += aView.frame.size.height;
	
	CGSize size = self.contentSize;
	CGPoint rb = aView.rightBottom;
	
	if (rb.x > size.width) size.width = rb.x;
	if (rb.y > size.height) size.height = rb.y;
	
	self.contentSize = size;
	
	[self addSubview:aView];
	return [self addMargin];
}

- (DescriptorView *)addMargin {
	CGSize size = self.contentSize;
	size.height += 6.0;
	self.contentSize = size;
	return self;
}

@end

